<!--
 * @Description: 设置审核人
 * @Autor: yjx
 * @Date: 2021-11-19 11:54:54
 * @LastEditors: yjx
 * @LastEditTime: 2021-11-19 11:54:54
-->
<template>
    <el-dialog title="标准设置" :visible="visible" width="400px" @close="onClose" @open="onOpen" @closed="onClosed">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="form">
            <el-form-item label="审核人：" prop="auditor">
                <el-select v-model="form.auditor" value-key="value" filterable placeholder="请选择">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button class="bottom_all_two" @click="onClose">取 消</el-button>
            <el-button class="bottom_all" type="primary" @click="onConfirm">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import { getUsetListByPermission } from '@/api/index.js'
    import { saveOrUpdate2 } from '@/api/qualityTemplate/index.js'
    export default {
        data() {
            return {
                options: [],
                form: {
                    auditor: ''
                },
                rules: {
                    auditor:[{ required: true, message: '请选择审核人'}]
                }
            };
        },
        props: {
            config: {
                type: Object,
                default: () => (
                    {type: 'input'}
                ),
            },
            // 控制dialog显示
            visible: {
                type: Boolean,
                default: false
            },
        },
        methods: {
            onOpen() { // 打开弹框，清除校验
                if(this.$refs.form) this.$refs.form.clearValidate();
                getUsetListByPermission().then(res=>{
                    this.options = res.data.map(item=>({
                        value: item.id,
                        label: item.name
                    }))
                })
            },
            onClose() { // 关闭弹框
                this.$emit('update:visible', false);
            },
            onConfirm() { // 确定
                this.$refs['form'].validate((valid) => {
                    if(valid) {
                        const params = {
                            type: this.config.type,
                            stationId: this.config.stationId,
                            nameId: this.config.nameId,
                            specificationId: this.config.specificationId,
                            reviewerId: this.form.auditor.value,
                            reviewer: this.form.auditor.label,
                        }
                        saveOrUpdate2(params).then(res=>{
                            if(res.code === 1001) {
                                this.$message.success("保存成功")
                            }
                            this.$emit('updated', false);
                            this.$emit('update:visible', false);
                        })
                    }
                });
            },
            onClosed() { // 关闭结束的回调，清空数据
                this.form.auditor = ''
            }
        },
    };
</script>

<style lang="scss" scoped>
</style>
